package com.victualler.core.common.web;

import com.victualler.core.common.dto.ResultDto;
import com.victualler.core.common.dto.WeiXinLoginDto;
import com.victualler.core.common.utils.HttpUtil;
import com.victualler.core.common.utils.JsonUtil;
import com.victualler.core.config.permission.entity.UserInfoDao;
import com.victualler.core.config.permission.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/")
public class LoginController {
	public static String  userSessionKey = "user";

	@Autowired
	private UserInfoService userInfoService;

	@Value("${appId:}")
	private String appId;

	@Value("${appSecret:}")
	private String appSecret;

	//登陆页
	@RequestMapping
	public String index(HttpSession session){
		if(session.getAttribute(userSessionKey)!=null){
			//重定向主页
			return "redirect:/show/page/common/index";
		}else{
			return "web/common/login";
		}
		
	}
	@RequestMapping(value = "/login",method = RequestMethod.GET)
	public String loginPage(){
		//跳转到login页面
		return "web/common/login";
	}

	/**
	 * @author Guoshichang
	 * @since 10:38 on 2018/9/22.
	 * @param code 从微信小程序发送过来的code代码
	 * @return String sessionId
	 * @Function
	 */
	@RequestMapping(value = "/loginWeiXin",method = RequestMethod.POST)
	@ResponseBody
	public ResultDto loginOnWeiXin(@RequestParam("code")String code , HttpSession session){
		try {
			//拿到微信登陆的code去请求微信服务器换取ip
			String weixinUrl = "https://api.weixin.qq.com/sns/jscode2session?appid="+this.appId+"&secret="+this.appSecret+"&js_code="+code+"&grant_type=authorization_code";
			String result = HttpUtil.postRequest(weixinUrl, null, "application/json;charset=UTF-8");
			WeiXinLoginDto weiXinLoginDto = JsonUtil.toJavaObject(result, WeiXinLoginDto.class);
			if (weiXinLoginDto.getErrcode() != null) {
				return new ResultDto(weiXinLoginDto.getErrmsg());
			} else {
				//设置服务端登陆标识
				session.setAttribute(userSessionKey, weiXinLoginDto.getOpenid());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new ResultDto(e.getMessage());
		}
		//反馈sessionId以便下次请求服务器时能够正常登陆
		ResultDto resultDto = new ResultDto();
		resultDto.setData(session.getId());
		return resultDto;
	}



	//登陆请求
	@RequestMapping(value = "/login",method = RequestMethod.POST)
	public String loginOn(@RequestParam("userId")String userId,@RequestParam("password")String password,HttpSession session){
		boolean  canLogin = true;
		//判断登录状态
		UserInfoDao user = userInfoService.getByUserId(userId);
		if(user !=null && user.getPassword().equals(password)){
			System.out.println("登录验证通过");
			canLogin = true;
		}else{
			System.out.println("登录验证失败");
			canLogin = false;
		}

		if(!canLogin){
			//重定向主页
			return "web/common/login"; 
		}else{
			session.setAttribute(userSessionKey, userId);
			System.out.println("获取id: "+session.getAttribute(userSessionKey));
			System.out.println("获取sessionId: "+session.getId());
			//重定向主页
			return "redirect:/show/page/common/index";
		}
	}
	//退出请求
	@RequestMapping("/loginOut")
	public String loginOut(HttpSession session){ 
		session.removeAttribute(userSessionKey);
		//重定向主页
		return "web/common/login"; 
	}
	
}
